package com.hxd.otc.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.utils.DataUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/6/12 0012.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/6/12 0012
 * description ${TODO}
 */
public class SecurityCenterItemView extends FrameLayout {
    @BindView(R.id.tv_left)
    TextView mTvLeft;
    @BindView(R.id.tv_right)
    TextView mTvRight;
    @BindView(R.id.switch_toggle)
    SwitchCompat mSwitchToggle;

    public SecurityCenterItemView(@NonNull Context context) {
        this(context, null);
    }

    public SecurityCenterItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SecurityCenterItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SecurityCenterItemView, defStyleAttr, 0);
        CharSequence leftText = a.getText(R.styleable.SecurityCenterItemView_leftText);
        CharSequence rightText = a.getText(R.styleable.SecurityCenterItemView_rightText);
        float leftTextSize = a.getDimension(R.styleable.SecurityCenterItemView_leftTextSize, 14);
        float rightTextSize = a.getDimension(R.styleable.SecurityCenterItemView_rightTextSize, 13);
        int leftTextColor = a.getColor(R.styleable.SecurityCenterItemView_leftTextColor, getResources().getColor(R.color.color_FF464E5F));
        int rightTextColor = a.getColor(R.styleable.SecurityCenterItemView_rightTextColor, getResources().getColor(R.color.color_FF464E5F));
        boolean isShowToggle = a.getBoolean(R.styleable.SecurityCenterItemView_isShowToggle, false);
        boolean toggleStatus = a.getBoolean(R.styleable.SecurityCenterItemView_toggleStatus, false);
        a.recycle();

        LayoutInflater.from(context).inflate(R.layout.view_security_center_item, this);
        setBackgroundResource(R.drawable.selector_item_click);
        setMinimumHeight(DataUtils.dp2px(getContext(), 44));
        ButterKnife.bind(this);

        setToggleStatus(toggleStatus);
        isShowToggle(isShowToggle);
        setLeftText(leftText);
        setLeftTextSize(leftTextSize);
        mLeftTextColor(leftTextColor);
        setRightText(rightText);
        setRightTextSize(rightTextSize);
        setRightTextColor(rightTextColor);
    }

    public void setToggleStatus(boolean toggleStatus) {
        mSwitchToggle.setChecked(toggleStatus);
    }

    private void isShowToggle(boolean isShowToggle) {
        mTvRight.setVisibility(isShowToggle ? GONE : VISIBLE);
        mSwitchToggle.setVisibility(isShowToggle ? VISIBLE : GONE);
    }

    public boolean isToggleTurnOn() {
        return mSwitchToggle.isChecked();
    }

    public void setLeftText(CharSequence leftText) {
        mTvLeft.setText(leftText);
    }

    public void setLeftTextSize(float leftTextSize) {
        mTvLeft.setTextSize(leftTextSize);
    }

    public void mLeftTextColor(int leftTextColor) {
        mTvLeft.setTextColor(leftTextColor);
    }

    public void setRightText(CharSequence rightText) {
        mTvRight.setText(rightText);
    }

    public void setRightTextSize(float rightTextSize) {
        mTvRight.setTextSize(rightTextSize);
    }

    public void setRightTextColor(int rightTextColor) {
        mTvRight.setTextColor(rightTextColor);
    }

    public String getRightText() {
        return mTvRight.getText().toString().trim();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}
